﻿package com.huanying.framework.user;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.aliyuncs.exceptions.ClientException;
import com.google.gson.Gson;
import com.huanying.framework.BaseController;
import com.huanying.framework.PageBean;
import com.huanying.framework.utils.Date;
import com.huanying.framework.utils.ImgCompress;
import com.huanying.framework.utils.MD5;
import com.huanying.framework.utils.SmsUtility;

@Controller
public class UserController extends BaseController {

	@Autowired
	private UserService userService;
	
	private static Logger logger = Logger.getLogger(UserController.class);

	//后台全部展示
	@RequestMapping(value = "/show_add_user.do")
	public String show_userInfo(String name, String sex, String phone, String address_home,String id_card,String beans,String money,String create_time,
		String status,String page_num, Model model) throws Exception {
        PageBean pageBean = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", name);
		map.put("sex", sex);
		map.put("phone", phone);
		map.put("address_home", address_home);
		map.put("id_card", id_card);
		map.put("money", money);
		map.put("beans", beans);	
		map.put("create_time", create_time);
		if (page_num == null) {
			page_num = "1";
		}
		try {
		     pageBean = userService.searchUsers(map,super.getPageSize(), Integer.valueOf(page_num));
		     logger.info("后台查询展示用户信息成功");
		} catch (NumberFormatException e) {
			logger.error("后台查询展示用户信息失败", e);
			//e.getMessage();
		} catch (Exception e) {
			logger.error("后台查询展示用户信息失败", e);
			//e.getMessage();
		}     
        model.addAttribute("name",name);
        model.addAttribute("sex",sex);
        model.addAttribute("phone",phone);
        model.addAttribute("address_home",address_home);
        model.addAttribute("id_card",id_card);
        model.addAttribute("money",money);
        model.addAttribute("beans",beans);
        model.addAttribute("create_time",create_time);
        
		model.addAttribute("users", pageBean.getList());
		model.addAttribute("page",pageBean);
		return "user/add_user";
	}
	//后台添加用户
	@RequestMapping(value="/add_user.do", method = RequestMethod.POST)
	public Map<String, String> save(@RequestBody User user) {
		Map<String, String> map = new HashMap<String, String>();
		try {
			if(user.getId()==0){
				userService.add(user);
			}
			else{
				userService.update(user);
			}
			map.put("flag", "1");
			map.put("id", String.valueOf(user.getId()));
			logger.info("后台添加用户成功");
			//userService.addCache(user);
			// TODO Auto-generated catch block
			//e1.printStackTrace();
		} catch (Exception e) {
			map.put("flag", "0");
			logger.error("后台添加用户失败",e);
		}
		return map;
	}
	//后台改变用户的个人实名状态
	@RequestMapping(value="/change_user_authenticPerTr.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> change_user_authenticPerTr(String id,String introduce,String flag) {
		Map<String, String> map = new HashMap<String, String>();
		User u;
		try {
			u = userService.queryCache(id);
			if(u!=null) {
			   }
			else {
			u = userService.getbyId(Integer.parseInt(id));									
			}
			if(Integer.parseInt(flag)==1) {
				u.setUser_authenticPer(1);		
			}
			else {
				u.setUser_authenticCom_errorIntro(introduce);
				u.setUser_authenticPer(0);		
			}
			
			userService.update(u);	
			map.put("flag", "1");
			logger.info("后台用户个人实名状态审核完成");
			
			userService.addCache(u);
			logger.info("后台用户个人实名状态审核完成时，在MemCache中添加");		
		} catch (Exception e) {
			map.put("flag", "0");
			logger.error("后台用户个人实名状态审核失败",e);
		}
		return map;
	}
	//后台改变用户的企业实名状态
	@RequestMapping(value="/change_user_authenticComTr.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> change_user_authenticComTr(String id,String introduce,String flag) {
		Map<String, String> map = new HashMap<String, String>();
		User u;
		try {
			u = userService.queryCache(id);
			if(u!=null) {
			   }
			else {
				u = userService.getbyId(Integer.parseInt(id));
		    }
			if(Integer.parseInt(flag)==1) {
				u.setUser_authenticCom(1);		
			}
			else {
				u.setUser_authenticCom_errorIntro(introduce);
				u.setUser_authenticCom(0);		
			}
			
			userService.update(u);	
			map.put("flag", "1");
			logger.info("后台用户个人实名状态审核完成");
			
			userService.addCache(u);
			logger.info("后台用户个人实名状态审核完成时，在MemCache中添加");
		} catch (Exception e) {
			map.put("flag", "0");
			logger.error("后台修改用户企业实名状态失败",e);
		}
		return map;
	}
	//后台改变用户的会员状态
	@RequestMapping(value="/change_is_vip.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> change_is_vip(String id, String is_vip) {
		Map<String, String> map = new HashMap<String, String>();
		User u = null;
		try {
			u = userService.queryCache(id);
			if(u!=null) {
			   }
			else {
				u = userService.getbyId(Integer.parseInt(id));
				}
			if(Integer.valueOf(is_vip)==1){
				u.setIs_vip(0);
			    map.put("is_vip", "0");
			}
			else {
				u.setIs_vip(1);
			    map.put("is_vip", "1");
			}
			userService.update(u);
			map.put("flag", "1");
			logger.info("后台改变用户的存在状态成功");
			
			userService.addCache(u);
			 logger.info("后台改变用户的会员状态时，在MemCache中添加");
		} catch (Exception e) {
			map.put("flag", "0");
			logger.error("后台改变用户的存在状态失败 ",e);
		}		
		return map;
	}	
	//后台改变用户的状态
	@RequestMapping(value="/change_user.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> save(String id, String status) {
		Map<String, String> map = new HashMap<String, String>();
		User u = null;
		try {
			u = userService.queryCache(id);
			if(u!=null) {
			   }
			else {
				u = userService.getbyId(Integer.parseInt(id));
				}
			if(Integer.valueOf(status)==1){
				u.setStatus(0);
			    map.put("status", "0");
			}
			else {
				u.setStatus(1);
			    map.put("status", "1");
			}		
			userService.update(u);
			map.put("flag", "1");
			logger.info("后台改变用户的存在状态成功");
			
			userService.addCache(u);
			 logger.info("后台改变用户的存在状态时，在MemCache中添加");
		} catch (Exception e) {
			map.put("flag", "0");
			logger.error("后台改变用户的存在状态失败 ",e);
		}
		return map;
	}
    //后台获取用户信息,查看详情页
	@RequestMapping(value = "/get_user.do", method = RequestMethod.GET) 
	public String show(String id, Model model) {
		try {
			User u = userService.getbyId(Integer.parseInt(id));
			model.addAttribute("user",u);
			logger.info("获取用户信息成功");
		} catch (Exception e) {
			logger.error("获取用户信息失败", e);
		}
		return "user/user_detail";
	}
    //后台获取个人实名认证信息
	@RequestMapping(value = "/get_userAuthenticPer.do", method = RequestMethod.GET) 
	public String showPer(String id, Model model) {
		try {

			User u = userService.getbyId(Integer.parseInt(id));	
			model.addAttribute("user", u);
			logger.info("后台获取个人实名认证信息成功");
		} catch (Exception e) {
			logger.error("后台获取个人实名认证信息失败", e);
		}
		return "user/authenticPer_detail";
	}
	 //后台获取企业实名认证信息
	@RequestMapping(value = "/get_userAuthenticCom.do", method = RequestMethod.GET) 
	public String showCom(String id, Model model) {
		try {
			User u = userService.getbyId(Integer.parseInt(id));				
			model.addAttribute("user", u);
			logger.info("后台获取企业实名认证信息成功");
		} catch (Exception e) {
			logger.error("后台获取企业实名认证信息失败", e);
		}
		return "user/authenticCom_detail";
	}	
	//后台获取个人认证审核中用户信息
	@RequestMapping(value = "/get_authenticPer.do", method = RequestMethod.GET) 
	public String get_authenticPer(String name,String page_num,Model model) {
		PageBean pageBean = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", name);
		if (page_num == null) {
			page_num = "1";
		}
		try {
		     pageBean = userService.searchUsersAuthenticPer(map,super.getPageSize(), Integer.valueOf(page_num));
		     logger.info("后台查询展示用户信息成功");
		} catch (NumberFormatException e) {
			logger.error("后台查询展示用户信息失败", e);
			//e.getMessage();
		} catch (Exception e) {
			logger.error("后台查询展示用户信息失败", e);
			//e.getMessage();
		}
		model.addAttribute("users", pageBean.getList());		
		model.addAttribute("name", name);	
		model.addAttribute("page",pageBean);
		return "user/authenticPer";
	}
	//后台获取企业认证审核中用户信息
	@RequestMapping(value = "/get_authenticCom.do", method = RequestMethod.GET) 
	public String get_authentic(String name,String page_num,Model model) {
		PageBean pageBean = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put("name",name);
		if (page_num == null) {
			page_num = "1";
		}
		try {
		     pageBean = userService.searchUsersAuthenticCom(map,super.getPageSize(), Integer.valueOf(page_num));
		     logger.info("后台获取企业认证审核中用户信息成功");
		} catch (NumberFormatException e) {
			logger.error("后台获取企业认证审核中用户信息失败", e);
			//e.getMessage();
		} catch (Exception e) {
			logger.error("后台获取企业认证审核中用户信息失败", e);
			//e.getMessage();
		}
		model.addAttribute("users", pageBean.getList());		
		model.addAttribute("name", name);
		model.addAttribute("page",pageBean);
		return "user/authenticCom";
	}
	//发送验证码
	@RequestMapping(value="/sendSms.do", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody 
	public String sendSms(String phone,String type,String jsoncallback) {
		logger.info(phone);
		logger.info(type);
		Map<String, Object> map = new HashMap<String, Object>();
		Map m = SmsUtility.sendSms(phone, Integer.parseInt(type));
		String codeSQL = m.get("code").toString();
		try {
			logger.info("已发送验证码");
			map.put("code", codeSQL);
			map.put("flag","1");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			map.put("flag","0");
			logger.error("发送验证码失败",e);
		}
		Gson gson=new Gson();
		String str=gson.toJson(map);
		return jsoncallback+"("+str+")";
	}
	//前台注册
	@RequestMapping(value="/add_newUser.do", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody 
	public String save_user(String phone,String password,String jsoncallback) {
		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(phone);
		try {
			User u = userService.getbyPhone(phone);
			map.put("flag", "1");
			logger.info("前台用户已注册");
		} 
		catch (Exception e) {
			// TODO Auto-generated catch block
			//e1.printStackTrace();
			User user = new User();
			user.setStatus(1);
			user.setPhone(phone);
			//生成5位随机数
			 StringBuffer flag = new StringBuffer();  
			for (int i = 0; i <= 50; i++)   
			{  
			    String sources = "0123456789"; // 加上一些字母，就可以生成pc站的验证码了  
			    Random rand = new Random();  
			    for (int j = 0; j < 5; j++)   
			    {  
			        flag.append(sources.charAt(rand.nextInt(9)) + "");  
			    }  
			}  
			String salt=flag.toString();
			user.setSalt(salt);
			user.setBeans(5);
			user.setIs_vip(0);
			
			//password=MD5.main(password,salt);	
			password = MD5.MD5Encode(password+salt);
			
			user.setPassword(password);
			user.setCreate_time(new Date());
			try {
				userService.add(user);
				logger.info("注册成功");
				map.put("flag", "2");
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				//e1.printStackTrace();
				map.put("flag", "0");
				logger.error("注册失败",e1);
			}
			try {
				userService.addCache(user);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				//e1.printStackTrace();
				logger.error("用户注册时，添加到MemCache失败",e);
			}
			}
		Gson gson=new Gson();
		String str=gson.toJson(map);
		return jsoncallback+"("+str+")";
	}
	
	//前台验证手机号是否已注册
	@RequestMapping(value="/search_newUser.do", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody 
	public String  search_user(String phone,String jsoncallback) {
		Map<String, String> map = new HashMap<String, String>();
		try {
			if(userService.getbyPhone(phone).getPhone()==null||userService.getbyPhone(phone).getPhone().equals("")){
			map.put("flag", "1");
			}else{
				map.put("flag", "2");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			map.put("flag", "0");
		}
		Gson gson=new Gson();
		String str=gson.toJson(map);
		System.out.println(str.getClass().getName());
		return jsoncallback+"("+str+")";
	}
	
	//前台手机号密码登录,返回用户信息
	@RequestMapping(value="/get_newUser.do", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody 
	public String get_user(String clientid,String token,String phone,String password,String jsoncallback) {
		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(clientid);
		logger.info(token);
		logger.info(phone);
		//logger.info(password);
		try {
			User u = userService.getbyPhone(phone);
			String salt=u.getSalt();
			//password=MD5.main(password,salt);
			password = MD5.MD5Encode(password+salt);
			
			//比对加密后的密码与数据库中的密码
			if(password.equals(u.getPassword())) {
				int id=u.getId();
				u.setClientid(clientid);
				if(token=="") {
				}else {
					u.setToken(token);
				}
				userService.update(u);
				map.put("flag", "1");
				map.put("user", u);     //用户的详细信息
				logger.info("登录成功");
			}else {
				map.put("flag", "2");
				logger.info("密码错误，登录失败");
			}		
		} catch (Exception e) {
				// TODO Auto-generated catch block
				//e1.printStackTrace();
			map.put("flag", "3");
			logger.error("手机号未注册,登录失败",e);
		}
		Gson gson=new Gson();
		String str=gson.toJson(map);
		return jsoncallback+"("+str+")";
	}	
	//前台输入用户id,返回用户信息
	@RequestMapping(value="/get_UserMes.do", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody 
	public String get_UserMes(String user_id,String jsoncallback) {
			Map<String, Object> map = new HashMap<String, Object>();
			logger.info(user_id);
			
			User u;
			try {
				u = userService.queryCache(user_id);
				if(u!=null) {
				   }
				else {
					u = userService.getbyId(Integer.parseInt(user_id));
					userService.addCache(u);
					 logger.info("前台输入用户id查找用户信息时，在MemCache中查找失败");	
				}
				map.put("user", u);     //用户的详细信息
			   logger.info("前台输入用户id,返回用户信息，查询成功");			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				map.put("flag", "2");
				logger.error("前台输入用户id,返回用户信息，查询失败",e);
			}
			Gson gson=new Gson();
			String str=gson.toJson(map);
			return jsoncallback+"("+str+")";
		}
	//前台手机号验证码登录
	@RequestMapping(value="/get_newUserIdentifyingCode.do", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody 
	public String get_userByIdentifyingCode(String clientid,String token,String phone,String jsoncallback) {
		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(clientid);
		logger.info(token);
		logger.info(phone);
		try {
			User u = userService.getbyPhone(phone);
			u.setClientid(clientid);
			if(token=="") {
			}else {
				u.setToken(token);
			}
			userService.update(u);
			map.put("flag", "1");
			map.put("user", u);     //用户的详细信息
			logger.info("前台手机号验证码登录，登录成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e1.printStackTrace();
			map.put("flag", "0");
			logger.error("前台手机号验证码登录，登录失败",e);
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		Gson gson=new Gson();
		String str=gson.toJson(map);
		return jsoncallback+"("+str+")";
	}
	//前台忘记密码,修改密码
	@RequestMapping(value="/update_userPassword.do", method = RequestMethod.GET)
	@ResponseBody 
	public String update_user(String phone,String password,String jsoncallback) {
		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(phone);
		logger.info(password);
		try {
			User u = userService.getbyPhone(phone);
			//生成5位随机数
			 StringBuffer flag = new StringBuffer();  
			for (int i = 0; i <= 50; i++)   
			{  
			    String sources = "0123456789"; // 加上一些字母，就可以生成pc站的验证码了  
			    Random rand = new Random();  
			    for (int j = 0; j < 5; j++)   
			    {  
			        flag.append(sources.charAt(rand.nextInt(9)) + "");  
			    }  
			}  
			String salt=flag.toString();
			u.setSalt(salt);
			//password=MD5.main(password,salt);
			password = MD5.MD5Encode(password+salt);
			
			u.setPassword(password);
			u.setCreate_time(new Date());
			userService.update(u);
			map.put("flag", "1");
			logger.info("前台修改密码成功");
			
			try {
				userService.addCache(u);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				//e1.printStackTrace();
				logger.error("前台修改密码时，加入MemCache失败",e1);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
		    //e1.printStackTrace();
		    map.put("flag", "2");
			logger.error("前台手机号未注册，修改密码失败",e);
		}
		Gson gson=new Gson();
		String str=gson.toJson(map);
		return jsoncallback+"("+str+")";
	}
	//发工长id,回是否认证
	@RequestMapping(value="/query_authentic.do", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody 
	public String query_authentic(String user_id,String jsoncallback) {
		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(user_id);
		User u=new User();
		try {
				u = userService.queryCache(user_id);
				if(u!=null) {
				   }
				else {
					u = userService.getbyId(Integer.parseInt(user_id));
					userService.addCache(u);
					 logger.info("输入用户id查找用户信息时，在MemCache中查找失败");	
					}
				//map.put("user", u);     //用户的详细信息
			   logger.info("查询成功");		
			if(u.getUser_authenticPer()==1) {
				map.put("flag", "1");
				logger.info("查询个人认证状态成功，用户已认证");
			}
		    if(u.getUser_authenticPer()==0) {
				map.put("flag", "2");
				logger.info("查询个人认证状态成功，用户未认证");
			}
		    if(u.getUser_authenticPer()==2) {
				map.put("flag", "3");
				logger.info("查询个人认证状态成功，用户审核中");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			map.put("flag", "0");
			logger.error("查询个人认证状态失败",e);
		}
		Gson gson=new Gson();
		String str=gson.toJson(map);
		return jsoncallback+"("+str+")";
	}
	//前台写入，修改，存入数据库
	@RequestMapping(value="/add_userMessage.do", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody 
	public String add_userMessage(String user_id,String birthday,String wechat,String address_home,String address_company,String jsoncallback) {
		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(user_id);
		logger.info(birthday);
		logger.info(wechat);
		logger.info(address_home);
		logger.info(address_company);
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			User u;
			u = userService.queryCache(user_id);
			if(u!=null) {
			   }
			else {
				u = userService.getbyId(Integer.parseInt(user_id));
				userService.addCache(u);
				 logger.info("前台写入修改，输入用户id查找用户信息时，在MemCache中查找失败");	
			}			
			u.setBirthday(sdf.parse(birthday));
			u.setWechat(wechat);
			u.setAddress_home(address_home);
			u.setAddress_company(address_company);
			userService.update(u);
			map.put("flag", "1");
			logger.info("前台写入用户信息，保存成功");
			
			userService.addCache(u);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
		    //e1.printStackTrace();
		    map.put("flag", "2");
			logger.error("写入用户信息，保存失败",e);
		}
		Gson gson=new Gson();
		String str=gson.toJson(map);
		return jsoncallback+"("+str+")";
	}	
	//前台个人实名认证，姓名，身份证号，正反面,个人身份认证
	@RequestMapping(value="/get_commit.do")
	@ResponseBody 
	public String get_commit(String user_id,String name,String sex,String id_card,String wechat,String jsoncallback,@RequestParam("image_01") MultipartFile image_01,@RequestParam("image_02") MultipartFile image_02,HttpServletRequest request) throws Exception { 
		Map<String, Object> map = new HashMap<String, Object>();
		String filePath_old1=null;
		String filePath_old2=null;
		logger.info(user_id);
		logger.info(name);
		logger.info(sex);
		logger.info(id_card);
		logger.info(wechat);
		//try {
		User u = userService.queryCache(user_id);
		if(u!=null) {
		   }
		else {
			u = userService.getbyId(Integer.parseInt(user_id));
			userService.addCache(u);
			 logger.info("前台输入用户id查找用户信息时，在MemCache中查找失败");	
			}	
		if(u.getUser_authenticPer()==1) {
			map.put("flag", "1");
			logger.info("前台用户已实名认证");
			
			Gson gson=new Gson();
    		String str=gson.toJson(map);
    		return jsoncallback+"("+str+")";
		}else {
			u.setName(name);
			u.setSex(sex);
			u.setId_card(id_card);
			u.setWechat(wechat);
			//存入身份证正反面
			String file1_name = "";
			String file2_name = "";
         if (!image_01.isEmpty()) {  
        	 try {  
        		String temp1_name = image_01.getOriginalFilename();
            	String postfix = temp1_name.substring(temp1_name.lastIndexOf("."), temp1_name.length());
            	file1_name = UUID.randomUUID().toString()+postfix;           	
            	filePath_old1 =super.getSession().getServletContext().getRealPath("")+File.separator+"fileUpload"+File.separator+"temp"+File.separator + file1_name;
            	String filePath_new1 = "fileUpload"+File.separator+"temp" +File.separator+ file1_name;
            	File oldFile1 = new File(filePath_old1);
                image_01.transferTo(oldFile1);
                logger.info(filePath_new1);
            	//ImgCompress.main(filePath_old1, filePath_old1);//压缩
                ImgCompress imgCom = new ImgCompress(filePath_old1);  
                imgCom.resizeFix(400, 400, filePath_old1);
                
                //oldFile1.delete();
                u.setId_card_front_url(filePath_new1);
                logger.info("身份证正面照保存成功");
            } catch (Exception e) {  
                //e.printStackTrace();  
            	map.put("flag", "3");
                logger.error("身份证正面照保存失败，个人实名认证失败",e);
                
                Gson gson=new Gson();
        		String str=gson.toJson(map);
        		return jsoncallback+"("+str+")";
            }  
            }  
         if (!image_02.isEmpty()) {  
                try {  
                	String temp2_name = image_02.getOriginalFilename();
                	String postfix = temp2_name.substring(temp2_name.lastIndexOf("."), temp2_name.length());
                	file2_name = UUID.randomUUID().toString()+postfix;
                	
                	filePath_old2 =super.getSession().getServletContext().getRealPath("")+"fileUpload" +File.separator +"temp"+File.separator + file2_name;
                	String filePath_new2 = "fileUpload" +File.separator +"temp"+File.separator + file2_name;
                	File oldFile2 = new File(filePath_old2);
                    image_02.transferTo(oldFile2);

                    logger.info(filePath_old2);
                    logger.info(filePath_new2);
                    //ImgCompress.main(filePath_old2, filePath_old2);//压缩
                    ImgCompress imgCom = new ImgCompress(filePath_old2);  
                    imgCom.resizeFix(400, 400, filePath_old2);
                    
                    //oldFile2.delete();
                    u.setId_card_back_url(filePath_new2);
                    logger.info("身份证反面照保存成功");
                    
                } catch (Exception e) {  
                    //e.printStackTrace();  
                    map.put("flag", "3");
        			logger.error("身份证反面照保存失败，个人实名认证失败",e);
        			
        			Gson gson=new Gson();
        			String str=gson.toJson(map);
        			return jsoncallback+"("+str+")";
                }  
         }
        try{
        	u.setUser_authenticPer(2);
            userService.update(u);
            map.put("flag", "5");
			logger.info("审核中");
			userService.addCache(u);
        }catch (Exception e) {
        	new File(filePath_old1).delete();
    		new File(filePath_old2).delete();
    		map.put("flag", "4");
    		logger.error("个人实名认证失败",e);
        }
    }
	Gson gson=new Gson();
	String str=gson.toJson(map);
	return jsoncallback+"("+str+")";
}
	//头像
	@RequestMapping(value="/get_headPortrait_url.do")
	@ResponseBody 
	public String get_headPortrait_url(String user_id,String jsoncallback, @RequestParam("image_01") MultipartFile image_01,HttpServletRequest request) throws Exception { 
		Map<String, Object> map = new HashMap<String, Object>();
		String filePath_old1=null;
		logger.info(user_id);
		User u= userService.queryCache(user_id);
		if(u!=null) {
		   }
		else {
			u = userService.getbyId(Integer.parseInt(user_id));
			userService.addCache(u);
			 logger.info("输入用户id查找用户信息时，在MemCache中查找失败");	
			}	

		//存入
		String file1_name = "";
        if (!image_01.isEmpty()) {  
                try {  
                	String temp1_name = image_01.getOriginalFilename();
                	String postfix = temp1_name.substring(temp1_name.lastIndexOf("."), temp1_name.length());
                	file1_name = UUID.randomUUID().toString()+postfix;
                	//super.getSession().getServletContext().getRealPath("");
                	filePath_old1 =super.getSession().getServletContext().getRealPath("") + File.separator + "fileUpload" + File.separator +"user_head"+ File.separator + file1_name;
                	String filePath_new1 = "fileUpload" + File.separator +"user_head"+ File.separator + file1_name;
                	File oldFile1 = new File(filePath_old1);
                    image_01.transferTo(oldFile1);
                    logger.info(filePath_new1);
                	//ImgCompress.main(filePath_old1, filePath_old1);//压缩
                    ImgCompress imgCom = new ImgCompress(filePath_old1);  
                    imgCom.resizeFix(400, 400, filePath_old1);
                    
                    //oldFile1.delete();
                    u.setHeadPortrait_url(filePath_new1);  
                } catch (Exception e) {  
                    //e.printStackTrace();  
                	map.put("flag", "0");
                    logger.error("头像保存失败",e);
                    
                    Gson gson=new Gson();
            		String str=gson.toJson(map);
            		return jsoncallback+"("+str+")";
                }  
            }  	

		try{
            userService.update(u);
			userService.addCache(u);
			map.put("flag", "1");
            logger.info("头像保存成功");
        }catch (Exception e) {
        	new File(filePath_old1).delete();
        	
        	map.put("flag", "0");
            logger.error("头像保存失败",e);
        }
		Gson gson=new Gson();
		String str=gson.toJson(map);
		return jsoncallback+"("+str+")";
	}
    //交互：企业实名认证，公司营业执照
	@RequestMapping(value="/get_commitCompany.do")
	@ResponseBody 
	public String get_commitCompany(String user_id,String business_license_num,String name_company,String address_company,String jsoncallback, 
	@RequestParam("image_01") MultipartFile image_01,HttpServletRequest request) throws Exception { 
	Map<String, Object> map = new HashMap<String, Object>();
	String filePath_old1=null;
	logger.info(business_license_num);
	logger.info(name_company);
	User u = userService.getbyId(Integer.parseInt(user_id));
	if(u.getUser_authenticCom()==1) {
		map.put("flag", "1");
		logger.info("企业已实名认证");
	}else {
		u.setBusiness_license_num(business_license_num);
		u.setName_company(name_company);
	    u.setAddress_company(address_company);
		
		//存入公司营业执照正反面
		String file1_name = "";
		if (!image_01.isEmpty()) {  
            try {  
            	String temp1_name = image_01.getOriginalFilename();
            	String postfix = temp1_name.substring(temp1_name.lastIndexOf("."), temp1_name.length());
            	file1_name = UUID.randomUUID().toString()+postfix;
            	//filePath_old1 = getSession().getServletContext().getRealPath("/") + "fileUpload" + File.separator +"user"+ File.separator + file1_name;
            	filePath_old1 = super.getSession().getServletContext().getRealPath("")+ File.separator + "fileUpload" + File.separator +"temp"+ File.separator + file1_name;
            	String filePath_new1 = "fileUpload" + File.separator +"temp"+ File.separator + file1_name;
            	File oldFile1 = new File(filePath_old1);
                image_01.transferTo(oldFile1);
                logger.info(filePath_old1);
                logger.info(filePath_new1);
            	//ImgCompress.main(filePath_old1, filePath_old1);//压缩
                ImgCompress imgCom = new ImgCompress(filePath_old1);  
                imgCom.resizeFix(400, 400, filePath_old1);
                
                //oldFile1.delete();

                u.setBusiness_license_front_url(filePath_new1);
                logger.info("企业执照正面照保存成功");
            } catch (Exception e) {  
                //e.printStackTrace();  
            	map.put("flag", "3");
                logger.error("企业执照正面照保存失败，企业实名认证失败",e);
                
                Gson gson=new Gson();
        		String str=gson.toJson(map);
        		return jsoncallback+"("+str+")";
            }  
        }    
        try{
        	u.setUser_authenticCom(2);
    		userService.update(u);		    
    	    map.put("flag", "5");
    		logger.info("企业实名认证审核中");
    		userService.addCache(u);
        }catch (Exception e) {
			// TODO Auto-generated catch block
			//e1.printStackTrace();
        	new File(filePath_old1).delete();
        	map.put("flag", "4");
			logger.error("企业实名认证时，加入MemCache失败",e);
		}
	}
	// TODO Auto-generated catch block
	//e2.printStackTrace();
	Gson gson=new Gson();
	String str=gson.toJson(map);
	return jsoncallback+"("+str+")";
	}
	
}
